package com.hejing.TCP与UDP网络编程;

import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * ClassName: TCPTest01
 * Package: com.hejing.TCP与UDP网络编程
 * Description:
 *  客户端发送内容给服务器，服务端将内容打印到控制台上
 * @Author: HeJing
 * @Create: 2023-06-02 19:37
 * @Version: 1.0
 */
public class TCPTest01 {

    /**
     * 客户端
     */
    @Test
    public void client() {
        Socket socket = null;
        OutputStream os = null;
        try {
            // 创建一个Socket
            InetAddress inetAddress = InetAddress.getByName("127.0.0.1");  // 声明服务端ip地址
            int port = 8989;  // 声明服务端端口号
            socket = new Socket(inetAddress, port);

            // 发送数据
            os = socket.getOutputStream();
            os.write("你好，我是客户端，请多指教".getBytes());  // getBytes()默认字符集为 utf-8
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭Socket
            try {
                if(socket != null)
                    socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(os != null)
                    os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 服务端
     */
    @Test
    public void server(){
        Socket socket = null;  // 阻塞式方法
        InputStream is = null;
        try {
            // 1、创建一个ServerSocket
            int port = 8989;  // 声明客户端的端口号
            ServerSocket serverSocket = new ServerSocket(port);

            // 2、调用accept(), 接收客户端的Socket
            socket = serverSocket.accept();
            System.out.println("服务器端已开启...");
            System.out.println("收到了来自" + socket.getInetAddress().getHostAddress() + "的连接");

            // 3、接收数据
            is = socket.getInputStream();
            byte[] buffer = new byte[1024];
            int size;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();  // 内部维护了一个byte[]数组，

            while((size = is.read(buffer)) != -1) {
                // 错误代码，可能会出现乱码:
                /*String str = new String(buffer, 0, size);
                System.out.print(str);*/

                // 正确代码:
                baos.write(buffer, 0, size);
            }
            System.out.println(baos.toString());
            System.out.println("数据接收完毕...");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 4、关闭Socket, is流
            try {
                if(socket != null)
                    socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(is != null)
                    is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}






















